package it.stasbranger.myproject.client.managed.ui;

import com.google.gwt.core.client.GWT;

import com.google.gwt.text.shared.AbstractRenderer;
import com.google.gwt.text.shared.Renderer;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;

import com.google.gwt.user.cellview.client.CellTable;

import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;

import com.google.gwt.user.cellview.client.TextColumn;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;

import it.stasbranger.myproject.client.managed.request.SuppliersProxy;

import it.stasbranger.myproject.client.scaffold.place.AbstractProxyListView;

import java.util.HashSet;
import java.util.Set;

/**
 *
 */
public class SuppliersListView extends AbstractProxyListView<SuppliersProxy> {
	interface Binder extends UiBinder<HTMLPanel, SuppliersListView> {
	}

	private static final Binder BINDER = GWT.create(Binder.class);

	private static SuppliersListView instance;

	@UiField
	CellTable<SuppliersProxy> table;
	@UiField
	Button newButton;

	private Set<String> paths = new HashSet<String>();

	public static SuppliersListView instance() {
		if (instance == null) {
			instance = new SuppliersListView();
		}

		return instance;
	}

	public SuppliersListView() {
		init(BINDER.createAndBindUi(this), table, newButton);
		table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.DISABLED);

		paths.add("idsuppliers");
		table.addColumn(new TextColumn<SuppliersProxy>() {
			Renderer<Integer> renderer = new AbstractRenderer<Integer>() {
				public String render(Integer obj) {
					return obj == null ? "" : String.valueOf(obj);
				}
			};

			@Override
			public String getValue(SuppliersProxy object) {
				return renderer.render(object.getIdsuppliers());
			}
		}, "Idsuppliers");
		paths.add("name");
		table.addColumn(new TextColumn<SuppliersProxy>() {
			Renderer<String> renderer = new AbstractRenderer<String>() {
				public String render(String obj) {
					return obj == null ? "" : String.valueOf(obj);
				}
			};

			@Override
			public String getValue(SuppliersProxy object) {
				return renderer.render(object.getName());
			}
		}, "Name");
		paths.add("address");
		table.addColumn(new TextColumn<SuppliersProxy>() {
			Renderer<String> renderer = new AbstractRenderer<String>() {
				public String render(String obj) {
					return obj == null ? "" : String.valueOf(obj);
				}
			};

			@Override
			public String getValue(SuppliersProxy object) {
				return renderer.render(object.getAddress());
			}
		}, "Address");
	}

	public String[] getPaths() {
		return paths.toArray(new String[paths.size()]);
	}
}
